package com.example.onlinereadingsystem.demos.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.onlinereadingsystem.demos.web.dto.domain.Novel;
import com.example.onlinereadingsystem.demos.web.dto.domain.NovelKind;
import com.example.onlinereadingsystem.demos.web.dto.vo.NovelDetailInfoVo;
import com.example.onlinereadingsystem.demos.web.service.KindService;
import com.example.onlinereadingsystem.demos.web.service.NovelKindService;
import com.example.onlinereadingsystem.demos.web.service.NovelService;
import com.example.onlinereadingsystem.demos.web.util.Result;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description 分类管理
 * @Author ljm
 * @Date 2024/10/14
 */
@Service
public class KindServiceImpl implements KindService {
    @Autowired
    NovelKindService novelKindService;
    @Autowired
    NovelService novelService;

    /**
     * @auther ljm
     * @create 2024/10/14
     * @describe 获取所有分类
     */
    @Override
    public Result<List<NovelKind>> getKindList() {
        List<NovelKind> list = novelKindService.list();
        return Result.OK(list);
    }

    /**
     * @auther ljm
     * @create 2024/10/14
     * @describe 通过分类id获取书籍信息
     */
    @Override
    public Result<List<NovelDetailInfoVo>> getKindDetail(String id) {
        LambdaQueryWrapper<Novel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Strings.isNotBlank(id), Novel::getNovelKindId, id);
        List<Novel> list = novelService.list(queryWrapper);
        List<NovelDetailInfoVo> res = new ArrayList<>();
        list.forEach(item -> {
            NovelDetailInfoVo novelDetailInfoVo = new NovelDetailInfoVo();
            BeanUtils.copyProperties(item, novelDetailInfoVo);
            novelDetailInfoVo.setNovelKind(novelKindService.getById(item.getNovelKindId()).getKindName());
            res.add(novelDetailInfoVo);
        });
        return Result.OK(res);
    }
}
